package com.mall.shopping.services;

import com.mall.shopping.ICartService;
import com.mall.shopping.converter.CartItemConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 创建日期: 2022/01/20 11:08
 *
 * @author WangZhe
 * 备注:
 */
@Slf4j
@Component
@Service
public class CartServcieImpl implements ICartService {
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    RedissonClient redissonClient;

    /**
     * @description 获取用户购物车列表
     * @author WangZhe
     * @date 2022/1/20 15:34
     * @param
     * @param request
     * @return com.mall.shopping.dto.CartListByIdResponse
     */
    @Override
    public CartListByIdResponse getCartListById(CartListByIdRequest request) {

        CartListByIdResponse response = new CartListByIdResponse();
        List<CartProductDto> productDtos = new ArrayList<>();
        Long userId = request.getUserId();
        RMap<Long, Item> map = redissonClient.getMap(userId + "");
        Set<Long> longs = map.keySet();
        for (Long key : longs) {
            Item item = map.get(key);
            CartProductDto cartProductDto = CartItemConverter.item2Dto(item);
            cartProductDto.setChecked("true");
            cartProductDto.setProductNum((long) item.getNum());
            productDtos.add(cartProductDto);
        }

        response.setCartProductDtos(productDtos);
        response.setCode("000000");
        response.setMsg("成功");

        return response;
    }
/**
 * @description 添加到购物车
 * @author WangZhe
 * @date 2022/1/20 16:33
 * @param
 * @param request
 * @return com.mall.shopping.dto.AddCartResponse
 */
    @Override
    public AddCartResponse addToCart(AddCartRequest request) {
        AddCartResponse response = new AddCartResponse();
        RMap<Object, Object> map = redissonClient.getMap("" + request.getUserId());
        Item item = itemMapper.selectByPrimaryKey(request.getItemId());
        item.setNum(request.getNum());
        map.put(request.getItemId(), item);
        response.setCode("000000");
        response.setMsg("成功");
        return response;
    }
/**
 * @description 更新购物车
 * @author WangZhe
 * @date 2022/1/20 16:33
 * @param
 * @param request
 * @return com.mall.shopping.dto.UpdateCartNumResponse
 */
    @Override
    public UpdateCartNumResponse updateCartNum(UpdateCartNumRequest request) {
        UpdateCartNumResponse response = new UpdateCartNumResponse();
        RMap<Object, Object> map = redissonClient.getMap("" + request.getUserId());
        Item item = (Item) map.get(request.getItemId());
        item.setNum(request.getNum());
        response.setCode("000000");
        response.setMsg("成功");
        return response;
    }

    @Override
    public CheckAllItemResponse checkAllCartItem(CheckAllItemRequest request) {
        return null;
    }

    @Override
    public DeleteCartItemResponse deleteCartItem(DeleteCartItemRequest request) {
        return null;
    }

    @Override
    public DeleteCheckedItemResposne deleteCheckedItem(DeleteCheckedItemRequest request) {
        return null;
    }

    @Override
    public ClearCartItemResponse clearCartItemByUserID(ClearCartItemRequest request) {
        return null;
    }

}
